<?php

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\PhpRedis;

class ActiveController extends BaseController
{

    private $activitySupermanLogic;

    private $activitySupermanCodeLogic;

    public function __construct()
    {
        parent::__construct();

        $this->activitySupermanLogic = D('ActivitySuperman', 'Logic');

        $this->activitySupermanCodeLogic = D('ActivitySupermanCode', 'Logic');
    }

    public function index()
    {
        $this->display();
    }

    public function getCode()
    {
        $status = I('post.status/d', 1);
        $page = I('post.p', 1);
        $pageSize = 10;
        $params = [];
        $params['code'] = I('post.code', '');
        $params['user_id'] = I('post.user_id');
        $params['mobile'] = I('post.mobile');
        $params['start'] = I('post.start');
        $params['end'] = I('post.end');

        if ($status == 1) {
            $data = $this->activitySupermanCodeLogic->getCodeList($page, $pageSize, $params);
            $tpl = 'not_send';
        } elseif ($status == 2) {
            $data = $this->activitySupermanLogic->getSendList($page, $pageSize, $params);
            $tpl = 'is_send';
        } else {
            $data = $this->activitySupermanLogic->getStandardList($page, $pageSize, $params);
            $tpl = 'do_send';
        }

        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);
        $this->assign('list', $data['list']);
        $this->display($tpl);
    }

    public function sendCode()
    {
        $redis = new PhpRedis(0);
        $keyName = getRedisKeyByCode('REDIS_KEY', '100000');
        $res = $redis->lPush($keyName,
            json_encode(
                [
                    'bizType'    => 8,
                    'bizContent' => []
                ]
            )
        );
        if ($res) {
            $this->helpJsonResult('200', '发放成功');
        }
        $this->helpJsonResult('400', '发放失败');
    }


    private function getCodeList($page, $pageSize, $params = [])
    {
        $where = '';
        if ($params['code']) {
            $where .= " AND act_code = '{$params['code']}'";
        }
        $model = M('activity_superman_code', 'clcw_', 'DB_CLCW');
        $list = $model->where("is_send = 0" . $where)->page($page, $pageSize)->select();
        $total = $model->where("is_send = 0" . $where)->count();
        return ['total' => $total, 'list' => $list];
    }

    private function getSendList($page, $pageSize, $params = [])
    {
        $where = "act_code != ''";
        if ($params['code']) {
            $where = "act_code = '{$params['code']}'";
        }
        if ($params['user_id']) {
            $where .= " AND user_id = '{$params['user_id']}'";
        }
        if ($params['mobile']) {
            $where .= " AND mobile = '{$params['mobile']}'";
        }
        if ($params['start']) {
            $start = strtotime($params['start']);
            $where .= " AND update_time > '$start'";
        }
        if ($params['end']) {
            $end = strtotime($params['end']);
            $where .= " AND update_time < '$end'";
        }
        $model = M('activity_superman', 'clcw_', 'DB_CLCW');
        $list = $model->where($where)->page($page, $pageSize)->select();
        $total = $model->where($where)->count();
        return ['total' => $total, 'list' => $list];
    }

    private function getStandardList($page, $pageSize, $params = [])
    {
        $where = "act_code = '' AND finish_time > 0 ";
        if ($params['user_id']) {
            $where .= " AND user_id = '{$params['user_id']}'";
        }
        if ($params['mobile']) {
            $where .= " AND mobile = '{$params['mobile']}'";
        }
        $model = M('activity_superman', 'clcw_', 'DB_CLCW');
        $list = $model->where($where)->page($page, $pageSize)->select();
        $total = $model->where($where)->count();
        return ['total' => $total, 'list' => $list];
    }
}
